package com.isyscore.iios.appdemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isyscore.boot.login.LoginUserManager;
import com.isyscore.iios.appdemo.mapper.CustomerMapper;
import com.isyscore.iios.appdemo.model.Customer;
import com.isyscore.iios.appdemo.service.CustomerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 客户服务实现
 * @author author
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    private final LoginUserManager userManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Customer saveCustomer(Customer customer) {
        //从permission-sdk中获取token对应用户登录名信息
        customer.setCreateBy(userManager.getCurrentLoginUser().getLoginName());
        customer.setCreateTime(LocalDateTime.now());
        //调用客户服务进行保存
        this.save(customer);
        return customer;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Customer updateCustomer(Customer customer) {
        //从permission-sdk中获取token对应用户登录名信息
        customer.setModiBy(userManager.getCurrentLoginUser().getLoginName());
        customer.setModiTime(LocalDateTime.now());
        //调用客户服务进行更新
        this.updateById(customer);
        return customer;
    }

    @Override
    public Customer getCustomer(String id) {
        return this.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteCustomer(String id) {
        this.removeById(id);
        return id;
    }
}
